package com.framework.foundation.handler;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.framework.foundation.util.HttpServletKit;
import com.google.common.collect.Maps;
import com.jfinal.core.JFinal;
import com.jfinal.handler.Handler;
import com.jfinal.render.FreeMarkerRender;

import freemarker.template.TemplateModelException;

/**
 * 全局Handler，设置一些通用功能
 * 
 * @author 董华健
 *         描述：主要是一些全局变量的设置
 */
public class GlobalHandler extends Handler {

	private static Logger log = Logger.getLogger(GlobalHandler.class);

	@Override
	public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		// 设置全局变量
		try {
			Map<String, Object> varibles = Maps.newHashMap();
			varibles.put("basePath", JFinal.me().getContextPath());// 设置项目根路径
			FreeMarkerRender.getConfiguration().setSharedVaribles(varibles);
		} catch (TemplateModelException e) {
			e.printStackTrace();
		}

		// 管理request
		HttpServletKit.setRequest(request);

		nextHandler.handle(target, request, response, isHandled);

	}

}
